home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-gmenu / GMenuSimpleEditor / menutreemodel.py < prev   
Encoding:
Python Source  |  2009-03-17  |  5.8 KB  |  172 lines

  1. #
  2. # Copyright (C) 2005 Red Hat, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. #
  18.  
  19. import os
  20. import os.path
  21. import gtk
  22. import gtk.gdk
  23. import gmenu
  24.  
  25. def lookup_system_menu_file (menu_file):
  26.     conf_dirs = None
  27.     if os.environ.has_key ("XDG_CONFIG_DIRS"):
  28.         conf_dirs = os.environ["XDG_CONFIG_DIRS"]
  29.     if not conf_dirs:
  30.         conf_dirs = "/etc/xdg"
  31.  
  32.     for conf_dir in conf_dirs.split (":"):
  33.         menu_file_path = os.path.join (conf_dir, "menus", menu_file)
  34.         if os.path.isfile (menu_file_path):
  35.             return menu_file_path
  36.     
  37.     return None
  38.  
  39. def load_icon_from_path (icon_path):
  40.     if os.path.isfile (icon_path):
  41.         try:
  42.             return gtk.gdk.pixbuf_new_from_file_at_size (icon_path, 24, 24)
  43.         except:
  44.             pass
  45.     return None
  46.  
  47. def load_icon_from_data_dirs (icon_value):
  48.     data_dirs = None
  49.     if os.environ.has_key ("XDG_DATA_DIRS"):
  50.         data_dirs = os.environ["XDG_DATA_DIRS"]
  51.     if not data_dirs:
  52.         data_dirs = "/usr/local/share/:/usr/share/"
  53.  
  54.     for data_dir in data_dirs.split (":"):
  55.         retval = load_icon_from_path (os.path.join (data_dir, "pixmaps", icon_value))
  56.         if retval:
  57.             return retval
  58.         retval = load_icon_from_path (os.path.join (data_dir, "icons", icon_value))
  59.         if retval:
  60.             return retval
  61.     
  62.     return None
  63.  
  64. def load_icon (icon_theme, icon_value):
  65.     if not icon_value:
  66.         return
  67.  
  68.     if os.path.isabs (icon_value):
  69.         icon = load_icon_from_path (icon_value)
  70.         if icon:
  71.             return icon
  72.         icon_name = os.path.basename (icon_value)
  73.     else:
  74.         icon_name = icon_value
  75.     
  76.     if icon_name.endswith (".png"):
  77.         icon_name = icon_name[:-len (".png")]
  78.     elif icon_name.endswith (".xpm"):
  79.         icon_name = icon_name[:-len (".xpm")]
  80.     elif icon_name.endswith (".svg"):
  81.         icon_name = icon_name[:-len (".svg")]
  82.     
  83.     try:
  84.         return icon_theme.load_icon (icon_name, 24, 0)
  85.     except:
  86.         return load_icon_from_data_dirs (icon_value)
  87.  
  88. class MenuTreeModel (gtk.TreeStore):
  89.     (
  90.         COLUMN_IS_ENTRY,
  91.         COLUMN_ID,
  92.         COLUMN_NAME,
  93.         COLUMN_ICON,
  94.         COLUMN_MENU_FILE,
  95.         COLUMN_SYSTEM_VISIBLE,
  96.         COLUMN_USER_VISIBLE
  97.     ) = range (7)
  98.  
  99.     def __init__ (self, menu_files):
  100.         gtk.TreeStore.__init__ (self, bool, str, str, gtk.gdk.Pixbuf, str, bool, bool)
  101.  
  102.         self.entries_list_iter = None
  103.         
  104.         self.icon_theme = gtk.icon_theme_get_default ()
  105.  
  106.         if (len (menu_files) < 1):
  107.             menu_files = ["applications.menu", "settings.menu"]
  108.  
  109.         for menu_file in menu_files:
  110.             tree = gmenu.lookup_tree (menu_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  111.             self.__append_directory (tree.root, None, False, menu_file)
  112.  
  113.             system_file = lookup_system_menu_file (menu_file)
  114.             if system_file:
  115.                 system_tree = gmenu.lookup_tree (system_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  116.                 self.__append_directory (system_tree.root, None, True, menu_file)
  117.  
  118.     def __append_directory (self, directory, parent_iter, system, menu_file):
  119.         if not directory:
  120.             return
  121.         
  122.         iter = self.iter_children (parent_iter)
  123.         while iter:
  124.             if self[iter][self.COLUMN_ID] == directory.menu_id:
  125.                 break
  126.             iter = self.iter_next (iter)
  127.  
  128.         if not iter:
  129.             iter = self.append (parent_iter)
  130.  
  131.             self[iter][self.COLUMN_IS_ENTRY] = False
  132.             self[iter][self.COLUMN_ID]       = directory.menu_id
  133.             self[iter][self.COLUMN_NAME]     = directory.name
  134.             self[iter][self.COLUMN_ICON]     = load_icon (self.icon_theme, directory.icon)
  135.  
  136.             if not menu_file is None:
  137.                 self[iter][self.COLUMN_MENU_FILE] = menu_file
  138.  
  139.         if system:
  140.             self[iter][self.COLUMN_SYSTEM_VISIBLE] = True
  141.         else:
  142.             self[iter][self.COLUMN_USER_VISIBLE]   = True
  143.         
  144.         for child_item in directory.contents:
  145.             if isinstance (child_item, gmenu.Directory):
  146.                 self.__append_directory (child_item, iter, system, None)
  147.                 
  148.             if not isinstance (child_item, gmenu.Entry):
  149.                 continue
  150.             
  151.             child_iter = self.iter_children (iter)
  152.             while child_iter:
  153.                 if child_item.type == gmenu.TYPE_ENTRY and \
  154.                    self[child_iter][self.COLUMN_IS_ENTRY] and \
  155.                    self[child_iter][self.COLUMN_ID] == child_item.desktop_file_id:
  156.                         break
  157.                 child_iter = self.iter_next (child_iter)
  158.  
  159.             if not child_iter:
  160.                 child_iter = self.append (iter)
  161.  
  162.                 self[child_iter][self.COLUMN_IS_ENTRY] = True
  163.                 self[child_iter][self.COLUMN_ID]       = child_item.desktop_file_id
  164.                 self[child_iter][self.COLUMN_NAME]     = child_item.name
  165.                 self[child_iter][self.COLUMN_ICON]     = load_icon (self.icon_theme,
  166.                                                                     child_item.icon)
  167.  
  168.             if system:
  169.                 self[child_iter][self.COLUMN_SYSTEM_VISIBLE] = not child_item.is_excluded
  170.             else:
  171.                 self[child_iter][self.COLUMN_USER_VISIBLE]   = not child_item.is_excluded
  172.